/*
 * Copyright (c) 2011, Michael Hengler (developer (at) hengler (dot) de),
 * Nikolaus Moll (developer (at) miblounge (dot) net).
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * * Neither the name of the Jombi project nor the
 * names of its contributors may be used to endorse or promote products
 * derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL JOMBI BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 */
package net.miblounge.jombi.impl.widgets;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import net.miblounge.jombi.impl.image.QtImageRegistry;
import net.miblounge.miglayout.qt.MigLayout;

import org.jowidgets.common.color.IColorConstant;
import org.jowidgets.common.image.IImageConstant;
import org.jowidgets.common.types.Cursor;
import org.jowidgets.common.types.Dimension;
import org.jowidgets.common.types.Position;
import org.jowidgets.common.types.Rectangle;
import org.jowidgets.common.widgets.IControlCommon;
import org.jowidgets.common.widgets.controller.IComponentListener;
import org.jowidgets.common.widgets.controller.IFocusListener;
import org.jowidgets.common.widgets.controller.IKeyListener;
import org.jowidgets.common.widgets.controller.IMouseListener;
import org.jowidgets.common.widgets.controller.IPopupDetectionListener;
import org.jowidgets.common.widgets.descriptor.IWidgetDescriptor;
import org.jowidgets.common.widgets.factory.ICustomWidgetCreator;
import org.jowidgets.common.widgets.factory.IGenericWidgetFactory;
import org.jowidgets.common.widgets.layout.ILayoutDescriptor;
import org.jowidgets.spi.impl.controller.PopupDetectionObservable;
import org.jowidgets.spi.impl.controller.TabItemObservableSpi;
import org.jowidgets.spi.widgets.IPopupMenuSpi;
import org.jowidgets.spi.widgets.ITabItemSpi;

import com.trolltech.qt.gui.QIcon;
import com.trolltech.qt.gui.QTabWidget;
import com.trolltech.qt.gui.QWidget;

public class TabItemImpl extends TabItemObservableSpi implements ITabItemSpi {

	private final QWidget tabContentContainer;
	private final QtContainer qtContainer;
	private final PopupDetectionObservable tabPopupDetectionObservable;
	private final Set<PopupMenuImpl> tabPopupMenus;

	private QTabWidget parentTabbedPane;
	private boolean detached;
	private String text;
	private String toolTipText;
	private IImageConstant icon;

	public TabItemImpl(final IGenericWidgetFactory genericWidgetFactory, final QTabWidget parentTabbedPane) {
		this(genericWidgetFactory, parentTabbedPane, null);
	}

	public TabItemImpl(final IGenericWidgetFactory genericWidgetFactory, final QTabWidget parentTabbedPane, final Integer index) {
		super();

		this.parentTabbedPane = parentTabbedPane;

		this.tabPopupDetectionObservable = new PopupDetectionObservable();
		this.tabPopupMenus = new HashSet<PopupMenuImpl>();

		this.tabContentContainer = new QWidget();
		this.tabContentContainer.setLayout(new MigLayout("", "[]", "[]"));

		if (index != null) {
			parentTabbedPane.insertTab(index.intValue(), tabContentContainer, "");
		} else {
			parentTabbedPane.addTab(tabContentContainer, "");
		}

		this.qtContainer = new QtContainer(genericWidgetFactory, tabContentContainer);

		registerEvents();
	}

	private void registerEvents() {}

	private void unregisterEvents() {}

	@Override
	public void addTabPopupDetectionListener(final IPopupDetectionListener listener) {
		tabPopupDetectionObservable.addPopupDetectionListener(listener);
	}

	@Override
	public void removeTabPopupDetectionListener(final IPopupDetectionListener listener) {
		tabPopupDetectionObservable.removePopupDetectionListener(listener);
	}

	@Override
	public IPopupMenuSpi createTabPopupMenu() {
		final PopupMenuImpl result = new PopupMenuImpl(tabContentContainer);
		tabPopupMenus.add(result);
		return result;
	}

	public void detach() {
		detached = true;
		parentTabbedPane.removeTab(getIndex());
		unregisterEvents();
	}

	public void attach(final QTabWidget parentTabbedPane, final Integer index) {
		this.detached = false;
		this.parentTabbedPane = parentTabbedPane;
		registerEvents();

		if (index != null) {
			parentTabbedPane.insertTab(index.intValue(), tabContentContainer, "");
		} else {
			parentTabbedPane.addTab(tabContentContainer, "");
		}

		setText(text);
		setToolTipText(toolTipText);
		setIcon(icon);
		//parentTabbedPane.setTabComponentAt(getIndex(), tabComponent);
	}

	public boolean isDetached() {
		return detached;
	}

	@Override
	public boolean isReparentable() {
		return true;
	}

	@Override
	public void setText(final String text) {
		this.text = text;
		if (text != null) {
			parentTabbedPane.setTabText(getIndex(), text);
		} else {
			parentTabbedPane.setTabText(getIndex(), String.valueOf(""));
		}
	}

	@Override
	public void setToolTipText(final String toolTipText) {
		this.toolTipText = toolTipText;
		parentTabbedPane.setTabToolTip(getIndex(), text);
	}

	@Override
	public void setIcon(final IImageConstant icon) {
		final int index = getIndex();
		final QIcon newIcon = QtImageRegistry.getInstance().getImage(icon);

		if (icon != newIcon) {
			this.icon = icon;
			parentTabbedPane.setTabIcon(index, newIcon);
		}
	}

	private int getIndex() {
		return parentTabbedPane.indexOf(tabContentContainer);
	}

	public QWidget getTabItem() {
		return tabContentContainer;
	}

	@Override
	public QWidget getUiReference() {
		return qtContainer.getUiReference();
	}

	@Override
	public void setEnabled(final boolean enabled) {
		qtContainer.setEnabled(enabled);
	}

	@Override
	public boolean isEnabled() {
		return qtContainer.isEnabled();
	}

	@Override
	public <WIDGET_TYPE extends IControlCommon> WIDGET_TYPE add(
		final Integer index,
		final IWidgetDescriptor<? extends WIDGET_TYPE> descriptor,
		final Object layoutConstraints) {
		return qtContainer.add(index, descriptor, layoutConstraints);
	}

	@Override
	public <WIDGET_TYPE extends IControlCommon> WIDGET_TYPE add(
		final Integer index,
		final ICustomWidgetCreator<WIDGET_TYPE> creator,
		final Object layoutConstraints) {
		return qtContainer.add(index, creator, layoutConstraints);
	}

	@Override
	public boolean remove(final IControlCommon control) {
		return qtContainer.remove(control);
	}

	@Override
	public IPopupMenuSpi createPopupMenu() {
		return qtContainer.createPopupMenu();
	}

	@Override
	public void redraw() {
		qtContainer.redraw();
	}

	@Override
	public void setForegroundColor(final IColorConstant colorValue) {
		qtContainer.setForegroundColor(colorValue);
	}

	@Override
	public void setBackgroundColor(final IColorConstant colorValue) {
		qtContainer.setBackgroundColor(colorValue);
	}

	@Override
	public IColorConstant getForegroundColor() {
		return qtContainer.getForegroundColor();
	}

	@Override
	public IColorConstant getBackgroundColor() {
		return qtContainer.getBackgroundColor();
	}

	@Override
	public void setCursor(final Cursor cursor) {
		qtContainer.setCursor(cursor);
	}

	@Override
	public void setVisible(final boolean visible) {
		qtContainer.setVisible(visible);
	}

	@Override
	public boolean isVisible() {
		return qtContainer.isVisible();
	}

	@Override
	public Dimension getSize() {
		return qtContainer.getSize();
	}

	@Override
	public void addPopupDetectionListener(final IPopupDetectionListener listener) {
		qtContainer.addPopupDetectionListener(listener);
	}

	@Override
	public void removePopupDetectionListener(final IPopupDetectionListener listener) {
		qtContainer.removePopupDetectionListener(listener);
	}

	@Override
	public void setLayout(final ILayoutDescriptor layoutDescriptor) {
		qtContainer.setLayout(layoutDescriptor);
	}

	@Override
	public void layoutBegin() {
		qtContainer.layoutBegin();
	}

	@Override
	public void layoutEnd() {
		qtContainer.layoutEnd();
	}

	@Override
	public void removeAll() {
		qtContainer.removeAll();
	}

	public void fireContextMenu(final Position pos) {
		tabPopupDetectionObservable.firePopupDetected(pos);
	}

	@Override
	public Position getPosition() {
		return qtContainer.getPosition();
	}

	@Override
	public void setPosition(final Position position) {
		qtContainer.setPosition(position);
	}

	@Override
	public void setSize(final Dimension size) {
		qtContainer.setSize(size);
	}

	@Override
	public void setRedrawEnabled(final boolean enabled) {
		qtContainer.setRedrawEnabled(enabled);
	}

	@Override
	public boolean requestFocus() {
		return qtContainer.requestFocus();
	}

	@Override
	public void addFocusListener(final IFocusListener listener) {
		qtContainer.addFocusListener(listener);
	}

	@Override
	public void removeFocusListener(final IFocusListener listener) {
		qtContainer.removeFocusListener(listener);
	}

	@Override
	public void addKeyListener(final IKeyListener listener) {
		qtContainer.addKeyListener(listener);
	}

	@Override
	public void removeKeyListener(final IKeyListener listener) {
		qtContainer.removeKeyListener(listener);
	}

	@Override
	public void addComponentListener(final IComponentListener componentListener) {
		qtContainer.addComponentListener(componentListener);
	}

	@Override
	public void removeComponentListener(final IComponentListener componentListener) {
		qtContainer.removeComponentListener(componentListener);
	}

	@Override
	public void addMouseListener(final IMouseListener mouseListener) {
		qtContainer.addMouseListener(mouseListener);
	}

	@Override
	public void removeMouseListener(final IMouseListener mouseListener) {
		qtContainer.removeMouseListener(mouseListener);
	}

	@Override
	public Rectangle getClientArea() {
		return qtContainer.getClientArea();
	}

	@Override
	public Dimension computeDecoratedSize(final Dimension clientAreaSize) {
		return qtContainer.computeDecoratedSize(clientAreaSize);
	}

	@Override
	public void setTabOrder(final List<? extends IControlCommon> tabOrder) {
		// TODO NM implement
	}
}
